﻿using StaffingSystem.Business;
using StaffingSystem.EF;
using StaffingSystem.Models;
using StaffingSystem.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace StaffingSystem.Controllers
{
    public class CheckController : Controller
    {
        // GET: Check

        public ActionResult Check()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Check(PageViewModel page)
        {

            string where = "";
            Pagelist pa = new Pagelist();
            if (page.Where != null)
            {

                ChecksCondition pc = new ChecksCondition();
                pc = JsonTool.JsonDeserialize<ChecksCondition>(page.Where);
                where += "Name like '%" + pc.name + "%'";
                if (pc.Checkss == "签到")
                {
                    where += " and AttendanceType=1";
                }
                else if (pc.Checkss == "签退")
                {
                    where += " and AttendanceType=0";
                }
                if (pc.YesorNO == "违纪")
                {
                    where += " and Violate=1";
                }
            }
            PageModel<checks> pagemodel = new PageModel<checks>();
            pagemodel.PageSize = page.PageSize;
            pagemodel.PageNumber = page.PageNumber;
            pagemodel.AscOrDesc = "";
            pagemodel.OrderID = "Id";
            pagemodel.PageKey = "Id";
            pagemodel.TableName = "checks";
            pagemodel.Where = where;
            pagemodel = pa.GetPageList(pagemodel);
            JsonResult result = new JsonResult();
            //调用存储过程
            Management db = new Management();
            StringBuilder procString = new StringBuilder();
            procString.Append("proc_datatime");
            procString.Append(" @jobnumber,");
            procString.Append("@recordCount out");
            SqlParameter recordCount = new SqlParameter("@recordCount", SqlDbType.Int);
            recordCount.Direction = ParameterDirection.Output;//表明此参数是输出型参数
            Loginon user = Session["JobNumber"] as Loginon;
            var num = db.Database.ExecuteSqlCommand(procString.ToString(), new object[]{
                    new SqlParameter("@jobnumber",user.JobNumber),
                    recordCount
            });
            //输出参数赋值给一个变量
            int aa = (int)recordCount.Value;
            if (pagemodel.DataList.Count == 0)
            {
                result.Data = new
                {
                    State = 0,
                    Message = "没有记录",
                    Data = "",
                    Checks = aa
                };
            }
            else
            {
                pagemodel.Where = "";
                result.Data = new
                {
                    State = 1,
                    Message = "获取数据成功",
                    Data = JsonTool.JsonSerializer(pagemodel),
                    Checks = aa
                };
            }

            return result;
        }
        [HttpPost]
        public ActionResult Button_show()
        {
            return View();
        }



        [HttpPost]
        public ActionResult signin()
        {
            personnelinfor cc = new personnelinfor();
            HttpContext.Session["user"] = cc as personnelinfor;
            JsonResult result = new JsonResult();
            Management db = new Management();
            StringBuilder procString = new StringBuilder();
            //声明存储过程
            procString.Append("insert_check");

            procString.Append(" @ID");
            Loginon user = Session["JobNumber"] as Loginon;
            
            SqlParameter[] parameters = {
                    new SqlParameter("@ID",user.JobNumber)
                };
            //调用存储过程
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), parameters);
            if (a > 0)
            {
                result.Data = new
                {
                    State = 1,
                    Message = "添加成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = 0,
                    Message = "添加失败"
                };
            }
            return result;

        }
    }
}